package com.project.dao.impl;

import com.project.bean.FacilitiesBean;
import com.project.bean.RoomBean;
import com.project.dao.IFacilitiesDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Repository
public class FacilitiesDaoImpl extends BaseDao implements IFacilitiesDao {
    @Override
    public List<FacilitiesBean> findByRoomId(Integer id) {
        List<FacilitiesBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_damageState where fk_roomId=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                list.add(this.getBean());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public void updateState(Integer id, String state) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_damageState set t_damageState = ? where pk_FacilitiesId=?");
            ps.setObject(1,state);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public void add(FacilitiesBean facilities) {
          this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_damageState(t_damage,t_damageDate,t_damageState,fk_roomId) values (?,?,?,?)");
            ps.setObject(1,facilities.getDamage());
            ps.setObject(2,facilities.getDamageDate());
            ps.setObject(3,facilities.getDamageState());
            ps.setObject(4,facilities.getRoomBean().getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void delRoom(Integer roomId) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_damageState where fk_roomId= ?");
            ps.setObject(1,roomId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public int findFacilitiesNum(Integer roomId, String state) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select count(*) c from t_damageState where fk_roomId = ? and t_damageState=?");
            ps.setObject(1,roomId);
            ps.setObject(2,state);
            rs = ps.executeQuery();
            while (rs.next()){
                return rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return 0;
    }

    public FacilitiesBean getBean(){
        FacilitiesBean bean = new FacilitiesBean();
        try {
           bean.setId(rs.getInt("pk_FacilitiesId"));
            String bir = rs.getString("t_damageDate");
            bean.setDamageDate(bir!=null? LocalDate.parse(bir):null);
            bean.setDamage(rs.getString("t_damage"));
            bean.setDamageState(rs.getString("t_damageState"));
            RoomBean bean1 = new RoomBean();
            bean1.setId(rs.getInt("fk_roomId"));
            bean.setRoomBean(bean1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bean;
    }

    public static void main(String[] args) {
        IFacilitiesDao dao = new FacilitiesDaoImpl();
//        System.out.println(dao.findByRoomId(4));
        System.out.println(dao.findFacilitiesNum(4,"未解决"));
//        dao.updateState(1,"未解决");
//        dao.add(new FacilitiesBean("hu",5));
//        dao.delRoom(5);
    }
}
